package com.lishu.carsafe.activity;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.lishu.carsafe.R;
import com.lishu.carsafe.model.entity.Alarm;
import com.lishu.carsafe.model.entity.AlarmStatistic;
import com.lishu.carsafe.model.entity.StatisticValue;
import com.lishu.carsafe.model.entity.VehicleId;
import com.lishu.carsafe.widget.SpinnerNaturePopWindow;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class StatisticsBaseActivity extends BaseActivity{
    //标题栏和tab
    protected LinearLayout title_right_layout, select_car_layout;
    protected TextView display_type;
    protected TextView select_date;
    //按月显示-布局
    protected LinearLayout layout_mile_duration_month, layout_power_charge_month, layout_schedule_task_month;
    //按年显示-布局
    protected LinearLayout layout_mile_duration_year, layout_power_charge_year, layout_schedule_task_year;
    //按月显示-文字提示
    private ImageView mile_icon, duration_icon;
    protected TextView mile_text, duration_text;
    private ImageView daily_power_icon, charge_num_icon;
    protected TextView daily_power_text, charge_num_text;
    private ImageView schedule_icon, task_icon;
    protected TextView schedule_text, task_text;
    private TextView schedule_task_unit, mile_duration_unit, power_charge_unit;
    protected TextView mile_duration_total, schedule_task_total, power_charge_total, alarm_total;
    //按年显示-文字提示
    protected TextView total_mile, total_duration, total_power, total_charge, total_schedule, total_task;
    //按月显示-饼状图
    //protected PieChart alarm_chart;
    //private final int ANIMATE_TIME = 1000;//动画时间//1秒钟

    protected void initView() {
        //标题栏
        select_car_layout = findViewById(R.id.select_car_layout);
        title_right_layout = findViewById(R.id.title_right_layout);
        display_type = findViewById(R.id.display_type);
        select_date = findViewById(R.id.select_date);
        //布局
        layout_mile_duration_month = findViewById(R.id.layout_mile_duration_month);
        layout_power_charge_month = findViewById(R.id.layout_power_charge_month);
        layout_schedule_task_month = findViewById(R.id.layout_schedule_task_month);
        layout_mile_duration_year = findViewById(R.id.layout_mile_duration_year);
        layout_power_charge_year = findViewById(R.id.layout_power_charge_year);
        layout_schedule_task_year = findViewById(R.id.layout_schedule_task_year);
        //按月显示-文字提示
        mile_icon = findViewById(R.id.mile_icon);
        duration_icon = findViewById(R.id.duration_icon);
        mile_text = findViewById(R.id.mile_text);
        duration_text = findViewById(R.id.duration_text);
        daily_power_icon = findViewById(R.id.daily_power_icon);
        charge_num_icon = findViewById(R.id.charge_num_icon);
        daily_power_text = findViewById(R.id.daily_power_text);
        charge_num_text = findViewById(R.id.charge_num_text);
        schedule_icon = findViewById(R.id.schedule_icon);
        task_icon = findViewById(R.id.task_icon);
        schedule_text = findViewById(R.id.schedule_text);
        task_text = findViewById(R.id.task_text);
        //按月显示-文字
        mile_duration_unit = findViewById(R.id.mile_duration_unit);
        power_charge_unit = findViewById(R.id.power_charge_unit);
        schedule_task_unit = findViewById(R.id.schedule_task_unit);
        mile_duration_total = findViewById(R.id.mile_duration_total);
        schedule_task_total = findViewById(R.id.schedule_task_total);
        power_charge_total = findViewById(R.id.power_charge_total);
        alarm_total = findViewById(R.id.alarm_total);
        //按月显示-饼状图
        //alarm_chart = findViewById(R.id.alarm_chart);
        //setPieData(alarm_chart, null);

        //按年显示-文字提示
        total_mile = findViewById(R.id.total_mile);
        total_duration = findViewById(R.id.total_duration);
        total_power = findViewById(R.id.total_power);
        total_charge = findViewById(R.id.total_charge);
        total_schedule = findViewById(R.id.total_schedule);
        total_task = findViewById(R.id.total_task);
    }

    /*private void initPieChart(PieChart chart){
        //Legend显示位置
        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        //l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setTextSize(11f);
        //去掉描述文字
        chart.getDescription().setEnabled(false);
        //去掉图中label
        chart.setDrawEntryLabels(false);

        chart.animateY(ANIMATE_TIME, Easing.EaseInOutQuad);
    }

    protected void setPieData(PieChart chart, AlarmStatistic alarmInfo) {
        if(alarmInfo == null){
            chart.clear();
            chart.setNoDataText("暂无数据");
            return;
        }

        List<Alarm> alarms = alarmInfo.getAlarms();
        int sumCount = alarmInfo.getSumCount();
        alarm_total.setText("总告警次数：" + sumCount + "次");

        if(sumCount < 1) {
            chart.clear();
            chart.setNoDataText("暂无数据");
            return;
        }
        initPieChart(chart);

        float total = (float) sumCount / 100L;
        List<PieEntry> entries = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        for(int i = 0; i < alarms.size(); i++){
            entries.add(new PieEntry((float)(alarms.get(i).getCount()) / total, alarms.get(i).getName()));
        }

        colors.add(getResources().getColor(R.color.statistics_blue));
        colors.add(getResources().getColor(R.color.statistics_yellow));
        colors.add(getResources().getColor(R.color.statistics_red));
        colors.add(getResources().getColor(R.color.tab_normal_bgd));
        colors.add(getResources().getColor(R.color.black));
        colors.add(getResources().getColor(R.color.main_color));
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);

        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setColors(colors);
        PieData pieData = new PieData(dataSet);
        //显示百分比
        //pieData.setDrawValues(true);
        chart.setUsePercentValues(true);
        pieData.setValueFormatter(new PercentFormatter(chart));
        //字体大小
        pieData.setValueTextSize(11f);
        pieData.setValueTextColor(Color.BLACK);

        //设置数据
        chart.setData(pieData);
        chart.invalidate();
    }*/

    protected void setMileAndDurationTotal(String type, String value){
        if("mile".equalsIgnoreCase(type)){
            mile_duration_total.setText("总行驶里程：" + value + "km");
        }else{
            mile_duration_total.setText("总运行时长：" + value + "h");
        }
    }

    protected void setPowerAndChargeTotal(String type, String value){
        if("power".equalsIgnoreCase(type)){
            power_charge_total.setText("总电耗量：" + value + "kw·h");
        }else{
            power_charge_total.setText("总充电次数：" + value + "次");
        }
    }

    protected void setScheduleAndTaskTotal(String type, String value){
        if("schedule".equalsIgnoreCase(type)){
            schedule_task_total.setText("总排班时长：" + value + "h");
        }else{
            schedule_task_total.setText("总完成任务次数：" + value + "次");
        }
    }

    protected void clickMileAndDuration(String type){
        if("mile".equalsIgnoreCase(type)){
            mile_icon.setImageResource(R.drawable.i_mile_green);
            duration_icon.setImageResource(R.drawable.hint_bgd);
            mile_text.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            duration_text.setTextColor(ContextCompat.getColor(mContext, R.color.light_gray));
            mile_duration_unit.setText("单位：km");
        }else{
            mile_icon.setImageResource(R.drawable.hint_bgd);
            duration_icon.setImageResource(R.drawable.i_mile_green);
            mile_text.setTextColor(ContextCompat.getColor(mContext, R.color.light_gray));
            duration_text.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            mile_duration_unit.setText("单位：h");
        }
    }

    protected void clickPowerAndCharge(String type){
        if("power".equalsIgnoreCase(type)){
            daily_power_icon.setImageResource(R.drawable.i_power_yellow);
            charge_num_icon.setImageResource(R.drawable.hint_bgd);
            daily_power_text.setTextColor(ContextCompat.getColor(mContext, R.color.statistics_yellow));
            charge_num_text.setTextColor(ContextCompat.getColor(mContext, R.color.light_gray));
            power_charge_unit.setText("单位：kw·h");
        }else{
            daily_power_icon.setImageResource(R.drawable.hint_bgd);
            charge_num_icon.setImageResource(R.drawable.i_power_yellow);
            daily_power_text.setTextColor(ContextCompat.getColor(mContext, R.color.light_gray));
            charge_num_text.setTextColor(ContextCompat.getColor(mContext, R.color.statistics_yellow));
            power_charge_unit.setText("单位：次");
        }
    }

    protected void clickScheduleAndTask(String type){
        if("schedule".equalsIgnoreCase(type)){
            schedule_icon.setImageResource(R.drawable.i_task_blue);
            task_icon.setImageResource(R.drawable.hint_bgd);
            schedule_text.setTextColor(ContextCompat.getColor(mContext, R.color.statistics_blue));
            task_text.setTextColor(ContextCompat.getColor(mContext, R.color.light_gray));
            schedule_task_unit.setText("单位：h");
        }else{
            schedule_icon.setImageResource(R.drawable.hint_bgd);
            task_icon.setImageResource(R.drawable.i_task_blue);
            schedule_text.setTextColor(ContextCompat.getColor(mContext, R.color.light_gray));
            task_text.setTextColor(ContextCompat.getColor(mContext, R.color.statistics_blue));
            schedule_task_unit.setText("单位：次");
        }
    }

    /**
     * 第一行1号显示在什么位置（星期几）
     */
    private int getFirstIndex(String month){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(str2Date(month));
        int firstIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        return firstIndex;
    }

    /**
     * 月份天数
     */
    private int getDayNumOfMonth(String month){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(str2Date(month));
        int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        return dayOfMonth;
    }

    private Date str2Date(String str){
        try {
            //SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            return df.parse(str);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    protected List<StatisticValue> transformToValueList(String month, List<String> mileagesList) {
        List<StatisticValue> tempList = new ArrayList<>();
        int firstIndex = getFirstIndex(month);
        int dayOfMonth = getDayNumOfMonth(month);
        for(int k = 0; k < firstIndex; k++){
            StatisticValue statisticValue = new StatisticValue();
            statisticValue.setDay(0);//不显示
            tempList.add(statisticValue);
        }
        for(int i = 1; i <= dayOfMonth; i++){//28
            StatisticValue statisticValue = new StatisticValue();
            statisticValue.setDay(i);
            if(mileagesList.size() >= i){//31
                statisticValue.setValue(mileagesList.get(i-1));
            }else{
                statisticValue.setValue("0");
            }

            tempList.add(statisticValue);
        }

        return tempList;
    }

}
